package com.ade.warehouse.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ade.warehouse.dao.RoleMenuDao;
import com.ade.warehouse.pojo.Menu;
import com.ade.warehouse.pojo.RoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.ade.warehouse.dao.RoleDao;
import com.ade.warehouse.pojo.Role;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RoleMenuDao roleMenuDao;
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 通过角色id查询菜单列表信息,并生成菜单树
	 * @return Result
	 */
	public List<Menu> getPermissionByRole(String roleId) {
		//获取所有的角色的权限列表
		String[] menuIds = getRoleById(roleId).getMenuIds();
		List<Menu> menuList = new ArrayList<>();
		for (String menuId:menuIds) {
			menuList.add(menuService.findById(menuId));
		}
		//排序
		Collections.sort(menuList, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				return o1.getSort() < o2.getSort() ? 1 : -1;//降序排列
			}
		});

		//查询其中的顶级分类
		List<Menu> rootMenuList = new ArrayList<>();
		for (Menu Menu: menuList) {
			if (Menu.getParentId().equals("0")) {
				rootMenuList.add(Menu);
			}
		}
		//生成树形菜单数据，为一级菜单设置子菜单，getChild是递归调用的
		for (Menu Menu : rootMenuList) {
			Menu.setChildren(menuService.getChild(Menu.getId(), menuList));
		}

		return rootMenuList;
	}

	public Role getRoleById(String roleId) {
		//查询角色信息
		Role role = roleDao.findById(roleId).get();

		//查询角色的权限信息

		List<RoleMenu> menuKeys = roleMenuDao.findByRoleId(roleId);

		String[] menuIds = new String[menuKeys.size()];
		for (int i = 0;i < menuKeys.size();i++) {
			menuIds[i] = menuKeys.get(i).getMenuId();
		}
		role.setMenuIds(menuIds);
		return role;
	}
	

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Role> findAll() {
		return roleDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Role> findSearch(Map whereMap, int page, int size) {
		Specification<Role> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return roleDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Role> findSearch(Map whereMap) {
		Specification<Role> specification = createSpecification(whereMap);
		return roleDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Role findById(String id) {
		return roleDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param role
	 */
	public void add(Role role) {
		role.setId( idWorker.nextId()+"" );
		roleDao.save(role);
	}

	/**
	 * 修改
	 * @param role
	 */
	public void update(Role role) {
		roleDao.save(role);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		roleDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Role> createSpecification(Map searchMap) {

		return new Specification<Role>() {

			@Override
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 
                if (searchMap.get("useable")!=null && !"".equals(searchMap.get("useable"))) {
                	predicateList.add(cb.like(root.get("useable").as(String.class), "%"+(String)searchMap.get("useable")+"%"));
                }
                // 
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}


}
